﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseUI;
using Utility;
using System.Data;
using Utility.Model;

namespace MsgCenter.Web.Pages.User
{
    public partial class Option_Look : BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strGuID = ParamsOfRequest.Request("Guid");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindRoles();
                this.GetModel();
                this.txtName.ToReadOnly();
                this.txtPassword.ToReadOnly();
                this.txtUserName.ToReadOnly();
                this.txtDeptName.ToReadOnly();
                this.chkIsAdmin.Disabled = true;
                this.listRole.Enabled = true;
            }
        }

        private void GetModel()
        {
            Sys_User model = BLL.BLLServiceHelper.GetUserService().GetModel(strGuID);
            if (model != null)
            {
                if (model.IsAdmin != null)
                {
                    this.chkIsAdmin.Checked = model.IsAdmin.Value;
                }
                this.txtName.Value = model.Name;
                this.txtUserName.Value = model.UserName;
                this.txtPassword.Value = model.Password.ToSafeDecrypt();
                if (model.DeptGuID != null)
                {
                    Sys_Dept modelDept = BLL.BLLServiceHelper.GetDeptService().GetModel(model.DeptGuID.ToSafeString());
                    if (modelDept != null)
                    {
                        this.txtDeptName.Value = modelDept.DeptName;
                    }
                }
            }
            using (DataTable dtUserRoles = BLL.BLLServiceHelper.GetUserService().GetUserRoles(strGuID))
            {
                foreach (DataRow dr in dtUserRoles.Rows)
                {
                    if (this.listRole.Items.FindByValue(dr["RoleID"].ToSafeString()) != null)
                    {
                        this.listRole.Items.FindByValue(dr["RoleID"].ToSafeString()).Selected = true;
                    }
                }
            }
        }

        private void BindRoles()
        {
            using (DataTable dtRole = BLL.BLLServiceHelper.GetRoleService().GetRoleTable(Sys_Role._.IsDelete == false))
            {
                foreach (DataRow dr in dtRole.Rows)
                {
                    this.listRole.Items.Add(new ListItem(dr["RoleName"].ToSafeString(), dr["RoleID"].ToSafeString()));
                }
            }
        }
    }
}